package com.smart.community.property.service.impl;

import com.smart.community.commons.entity.property.PropertyCompany;
import com.smart.community.feign.property.dto.PropertyCompanyPaymentInfoDTO;
import com.smart.community.property.mapper.PropertyCompanyMapper;
import com.smart.community.property.service.IPropertyCompanyPaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 物业公司支付服务实现类
 * 
 * @author Wu.Liang
 * @version 1.0.0
 * @since 2025-01-30
 */
@Slf4j
@Service
public class PropertyCompanyPaymentServiceImpl implements IPropertyCompanyPaymentService {

    @Autowired
    private PropertyCompanyMapper propertyCompanyMapper;

    @Override
    public PropertyCompanyPaymentInfoDTO getPropertyCompanyPaymentInfo(Long propertyCompanyId) {
        log.debug("获取物业公司支付信息，物业公司ID: {}", propertyCompanyId);
        
        if (propertyCompanyId == null) {
            return createDefaultPaymentInfo(propertyCompanyId);
        }
        
        try {
            PropertyCompany propertyCompany = propertyCompanyMapper.selectById(propertyCompanyId);
            if (propertyCompany == null) {
                return createDefaultPaymentInfo(propertyCompanyId);
            }
            
            return convertToPaymentInfoDTO(propertyCompany);
        } catch (Exception e) {
            log.error("获取物业公司支付信息失败，物业公司ID: {}", propertyCompanyId, e);
            return createDefaultPaymentInfo(propertyCompanyId);
        }
    }

    @Override
    public Map<Long, PropertyCompanyPaymentInfoDTO> getPropertyCompanyPaymentInfoBatch(List<Long> propertyCompanyIds) {
        log.debug("批量获取物业公司支付信息，物业公司ID数量: {}", propertyCompanyIds.size());
        
        if (CollectionUtils.isEmpty(propertyCompanyIds)) {
            return new HashMap<>();
        }
        
        try {
            List<PropertyCompany> propertyCompanies = propertyCompanyMapper.selectBatchIds(propertyCompanyIds);
            
            Map<Long, PropertyCompanyPaymentInfoDTO> paymentInfoMap = propertyCompanies.stream()
                    .collect(Collectors.toMap(
                            PropertyCompany::getId,
                            this::convertToPaymentInfoDTO
                    ));
            
            for (Long propertyCompanyId : propertyCompanyIds) {
                if (!paymentInfoMap.containsKey(propertyCompanyId)) {
                    paymentInfoMap.put(propertyCompanyId, createDefaultPaymentInfo(propertyCompanyId));
                }
            }
            
            return paymentInfoMap;
        } catch (Exception e) {
            log.error("批量获取物业公司支付信息失败", e);
            return createDefaultBatchPaymentInfo(propertyCompanyIds);
        }
    }
    
    private PropertyCompanyPaymentInfoDTO convertToPaymentInfoDTO(PropertyCompany propertyCompany) {
        PropertyCompanyPaymentInfoDTO paymentInfo = new PropertyCompanyPaymentInfoDTO();
        BeanUtils.copyProperties(propertyCompany, paymentInfo);
        
        paymentInfo.setPropertyCompanyId(propertyCompany.getId());
        paymentInfo.setOnlinePaymentEnabled(true);
        paymentInfo.setQrCodePaymentEnabled(propertyCompany.getPaymentQrCode() != null);
        paymentInfo.setPaymentDescription("支持多种支付方式");
        
        return paymentInfo;
    }
    
    private PropertyCompanyPaymentInfoDTO createDefaultPaymentInfo(Long propertyCompanyId) {
        PropertyCompanyPaymentInfoDTO defaultInfo = new PropertyCompanyPaymentInfoDTO();
        defaultInfo.setPropertyCompanyId(propertyCompanyId);
        defaultInfo.setCompanyName("物业公司");
        defaultInfo.setOnlinePaymentEnabled(false);
        defaultInfo.setQrCodePaymentEnabled(false);
        defaultInfo.setPaymentDescription("支付服务暂时不可用");
        return defaultInfo;
    }
    
    private Map<Long, PropertyCompanyPaymentInfoDTO> createDefaultBatchPaymentInfo(List<Long> propertyCompanyIds) {
        Map<Long, PropertyCompanyPaymentInfoDTO> defaultMap = new HashMap<>();
        for (Long propertyCompanyId : propertyCompanyIds) {
            defaultMap.put(propertyCompanyId, createDefaultPaymentInfo(propertyCompanyId));
        }
        return defaultMap;
    }
}
